<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="项目档案编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="finalNo">
              <a-input :disabled="true" v-model="model.finalNo" placeholder="请输入项目档案编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input :disabled="formDisabled" v-model="model.projectName" placeholder="请输入项目名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同最新进度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNodeId_dictText">
              <a-input :disabled=formDisabled v-model="model.contractNodeId_dictText" placeholder="请输入合同最新进度"></a-input>

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="最近更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractLastOperTime">
              <j-date :disabled=formDisabled placeholder="请选择最近更新时间" v-model="model.contractLastOperTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractCompleteStatus_dictText">
              <a-input :disabled=formDisabled v-model="model.contractCompleteStatus_dictText" placeholder="请输入状态"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="48">
          <a-col :span="10">
            <a-card    :bordered="false">
              <a-steps  direction="vertical" v-model="current" @change="onChange" :style="stepStyle">
                <template v-for="(item,index) in nodeList">
                  <a-step :title=item.nodeName :description= item.nodeProgress :subTitle=item.completeTime
                          :status="item.choose==1? 'process': (item.contractCompleteStatus==3 ? 'finish' :'wait')"  />
                </template>
              </a-steps>
            </a-card>
          </a-col>
          <a-col :span="14">
            <a-card :title="formTitle" :bordered="false">
              <a-form-model-item label="进度节点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeName">
                <a-input :disabled=formDisabled  v-model="model.nodeName" placeholder="请输入进度节点名称" style="width: 100%" />
              </a-form-model-item>
              <a-form-model-item label="进度节点说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeDesc">
                <a-textarea :disabled="formDisabled" v-model="model.nodeDesc" rows="6" placeholder="请输入进度节点说明" />
              </a-form-model-item>
              <a-form-model-item label="关联任务进度节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskNodeText">
                <!-- <select-progress-contract-detail-input ref="detail_input" v-model="model.taskNodeId" :multi="true" /> -->
                <a-input :disabled=formDisabled  v-model="model.taskNodeText" placeholder="请输入关联任务进度节点" style="width: 100%" />
              </a-form-model-item>
              <a-form-model-item label="完成状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractCompleteStatus">
                <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.contractCompleteStatus" dictCode="task_complete_status" placeholder="请选择完成状态" />
              </a-form-model-item>
              <a-form-model-item label="完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="completeTime">
                <j-date :disabled="formDisabled" placeholder="请选择完成时间" v-model="model.completeTime" :show-time="true"
                        date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-model-item>
              <a-form-model-item label="完成情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="completeInfo">
                <a-textarea :disabled="formDisabled" v-model="model.completeInfo" rows="6" placeholder="请输入完成情况" />
              </a-form-model-item>
              <a-form-model-item label="节点附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeFile">
                <j-upload  v-if="!formDisabled" v-model="model.nodeFile"></j-upload>
                <a-button v-else icon="download" @click="viewFile(model.nodeFile)">节点附件</a-button>
              </a-form-model-item>
            </a-card>
          </a-col>
        </a-row>
      </a-form-model>
      <view-file-modal ref="viewFileModal"></view-file-modal>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue, viewFile } from '@/utils/util'
  import ViewFileModal from '@/views/scy/components/ViewFileModal'
  import SelectProgressContractDetailInput from '@/views/scy/components/SelectProgressContractDetailInput'

  export default {
    name: 'ErpProgressContractViewsForm',
    mixins: [viewFile],
    components: {
      ViewFileModal,
      SelectProgressContractDetailInput
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: true,
      }
    },
    data () {
      return {
        stepStyle: {
          marginBottom: '60px',
          boxShadow: '0px -1px 0 0 #e8e8e8 inset',
        },
        formTitle: '',
        current: 0,
        nodeList: [],
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/api/erp/progress/erpProgressTaskNode/add",
          edit: "/api/erp/progress/erpProgressTaskNode/edit",
          queryById: "/api/erp/progress/erpProgressTaskNode/queryById",
          queryListByProjectId: "/api/erp/progress/erpProgressContractNode/queryListByContractId",
          queryListByTaskByIds: '/api/erp/progress/erpProgressTaskNode/queryListByTaskByIds'
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      onChange(current) {
        console.log(current,"=====================");
        this.nodeList[current].choose='1'
        for (var i = 0 ; i<this.nodeList.length;i++){
          if (i!=current){
            this.nodeList[i].choose='0'
          }
        }
        // this.model = {}
        this.formTitle = this.nodeList[current].nodeName
        // this.model = this.nodeList[current]
        this.getNodeListText(this.nodeList[current].taskNodeId,current)
        this.current = current;
      },
      add () {
        this.current = 0
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        console.log(record,"======================");
        this.model = Object.assign({}, record);
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
        // 查看节点列表
        if (this.model.id) {
          getAction(this.url.queryListByProjectId, { id: this.model.id }).then((res) => {
            if (res.success) {
              this.nodeList = res.result || []
              // this.model = this.nodeList[0]
              if(this.nodeList.length>0) {
                this.nodeList[0].choose = '1'
                this.formTitle = this.nodeList[0].nodeName
                this.getNodeListText(this.nodeList[0].taskNodeId,0)
                this.current = 0
              }
            } else {
              this.nodeList = []
              this.formTitle = ''
            }
          })
        }
        this.visible = true;
      },
      getNodeListText(ids,index) {
        var text = [],fileList = []
        if (!ids) {
          return
        }
        let param = {
          taskNodeIds: ids
        }
        getAction(this.url.queryListByTaskByIds, param).then((res) => {
          if (res.success) {
            console.log("huixai==========", res);
            // this.selectedObj = res.result || []
            if(res.result) {

              res.result.forEach((item,index)=>{
                  text.push(item.nodeName)
                  fileList.push(item.nodeFile)
              })
            }
            this.model = Object.assign(this.modelDefault, this.nodeList[index]);
            this.model.taskNodeText = text.toString()
            this.model.nodeFile = fileList.toString()
            console.log(this.model,"this.modelthis.modelthis.modelthis.model");
            // this.$refs.inputModal.add(this.selectedObj, this.ids);
          }
        });
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = this.url.edit;
            let method = 'put';
            this.model.id = this.nodeList[this.current].id
            this.model.isFill = '1'
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>
<style>
.ant-steps ant-steps-vertical{
  margin-bottom: 60px;
}
</style>